package com.website.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.website.common.controller.BaseController;
import com.website.common.warpper.ResultMap;
import com.website.system.model.DrugsType;
import com.website.system.service.DrugsTypeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@Controller
@RequestMapping("manage/drugsType")
public class DrugsTypeController extends BaseController {
    @Resource
    private DrugsTypeService drugsTypeService;
    /**
     * 查看所有
     */
    @RequestMapping("/selectType")
    @ResponseBody
    public List<DrugsType> selectType() {
        List<DrugsType> drugsTypes = drugsTypeService.selectList(new EntityWrapper<>());
        return drugsTypes;
    }
    @RequestMapping("")
    public String drugsType() {
        return "manage/drugsType";
    }
    @ResponseBody
    @RequestMapping("/DrugsTypeTable")
    public ResultMap drugsTypeTable(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam(required = false) String type_name) {
        ResultMap<List<DrugsType>> resultMap = new ResultMap<>(SUCCESS_CODE, SUCCESS_MSG, null, null);
        Page<DrugsType> pageInfo = new Page<>(page, limit);
        Wrapper<DrugsType> wrapper = new EntityWrapper<>();
        wrapper = type_name == null ? wrapper : wrapper.like("type_name", type_name);
        List<DrugsType> drugstype = drugsTypeService.selectPage(pageInfo, wrapper).getRecords();
        resultMap.setCount(drugsTypeService.selectCount(wrapper));
        resultMap.setData(drugstype);
        return resultMap;
    }
    //刪除
    @ResponseBody
    @RequestMapping(value = "/deleteDrugsType", method = RequestMethod.POST)
    public String deleteDrugsType(@Param("id") Integer id) {
        try {
            String name = drugsTypeService.selectById(id).getTypeName();
            deleteFile(name);
            drugsTypeService.deleteById(id);
            return SUCCESS_MSG;
        } catch (Exception e) {
            return ERROR_MSG;
        }
    }
    //查看
    @RequestMapping("/toReadDrugsType/{type}")
    public String Read(Model model, @PathVariable Integer type) {
        model.addAttribute("typeName", drugsTypeService.selectById(type));
        return "manage/readDrugsType";
    }
    /**
     * 修改页面信息
     */
    @RequestMapping("/toUpdateDrugsType/{type}")
    public String getAssociation(Model model, @PathVariable Integer type) {
        model.addAttribute("drugsType", drugsTypeService.selectById(type));
        return "manage/updateDrugsType";
    }
    /**
     * 保存修改
     */
    @ResponseBody
    @RequestMapping("/updateDrugsType")
    public String saveUpd(DrugsType drugsType) {
        try {
            DrugsType drugsT = drugsTypeService.selectById(drugsType.getId());
            drugsT.setTypeName(drugsType.getTypeName());
            drugsTypeService.updateById(drugsT);
            return SUCCESS_MSG;
        } catch (Exception e) {
            return ERROR_MSG;
        }
    }
    /**
     * 跳转到添加页面
     */
    @RequestMapping("/toAddDrugsTypes")
    public String addDrugsType() {
        return "manage/addDrugsType";
    }
    /**
     * 保存添加
     */
    @ResponseBody
    @RequestMapping("/addDrugsTypeSave")
    public String addRolesOne(DrugsType drugsType) {
        drugsTypeService.insert(drugsType);
        return "添加成功";
    }
}
